﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.Web.ASPxEditors;

public partial class SMSManage_EditSMSParameter : Frame.Pages.BasePage
{
    string[] MappingValueName = { "COM", "BaudRate", "Rate", "Book", "Cancel", "Balance", "Order", "Mday" };
    protected override void Page_Load(object sender, EventArgs e)
    {
        base.Page_Load(sender, e);
        if (!IsPostBack)//判断是否有激发postback 在本页面中即按钮按键的postback的click事件
        {
            //构建LTS数据上下文
            using (DAL.CMSDALDataContext Dcontext = new DAL.CMSDALDataContext())
            {
                //遍历MappingValueName 数组
                foreach (string item in MappingValueName)
                {
                    //获取特定的系统设置对象（数据相关）
                    DAL.BI_SysSettings setting =
                        Dcontext.BI_SysSettings.FirstOrDefault(o => o.SettingName == string.Format("SMS_{0}", item));
                    if (setting != null)//判断是否为空
                    {
                        //通过控件名称 做控件查找 也可以直接通过变量去的 如Detailpanel
                        ASPxTextBox txt = Detailpanel.FindControl(string.Format("txt_{0}", item)) as ASPxTextBox;
                        if (txt != null)
                        {
                            txt.Text = setting.Value;
                        }
                        else
                        {
                            ASPxComboBox cbo = Detailpanel.FindControl(string.Format("cbo_{0}", item)) as ASPxComboBox;
                            if (cbo != null)
                            {
                                cbo.Text = setting.Value;
                            }
                            else
                            {
                                ASPxTimeEdit te = Detailpanel.FindControl(string.Format("te_{0}", item)) as ASPxTimeEdit;
                                te.Text = setting.Value;
                            }
                        }
                    }
                }
            }
        }
    }

    protected void SubmitBtn_Click(object sender, EventArgs e)
    {
        //错误抓取操作
        try
        {
            using (DAL.CMSDALDataContext Dcontext = new DAL.CMSDALDataContext())
            {
                foreach (string item in MappingValueName)
                {
                    DAL.BI_SysSettings setting = Dcontext.BI_SysSettings.FirstOrDefault(o => o.SettingName == string.Format("SMS_{0}", item));
                    ASPxTextBox txt = Detailpanel.FindControl(string.Format("txt_{0}", item)) as ASPxTextBox;
                    if (txt != null)
                    {
                        setting.Value = txt.Text;
                    }
                    else
                    {
                        ASPxComboBox cbo = Detailpanel.FindControl(string.Format("cbo_{0}", item)) as ASPxComboBox;
                        if (cbo != null)
                        {
                            setting.Value = cbo.Text;
                        }
                        else
                        {
                            ASPxTimeEdit te = Detailpanel.FindControl(string.Format("te_{0}", item)) as ASPxTimeEdit;
                            setting.Value = te.Text;
                        }
                    }
                }
                Dcontext.SubmitChanges();
            }
            Frame.Global.GlobalFunctions.Alert("参数设置成功，请重启后台处理程序！");
        }
        catch (Exception)
        {
            throw;
        }

    }

    protected override string ActionID
    {
        get { return "0"; }
    }
}